﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autofac;
using Sahit.iSoilHotel.Infrastructure.Web.DependencyManagement;
using Sahit.iSoilHotel.Infrastructure.Web.Configuration;
using System.Configuration;
using Sahit.iSoilHotel.Infrastructure.Web.Task;

namespace Sahit.iSoilHotel.Infrastructure.Web.Framework
{
    public class SoilEngine : IEngine
    {
        #region Fields

        private ContainerManager _containerManager;

        #endregion

        #region Ctor

        public SoilEngine()
            : this(EventBroker.Instance, new ContainerConfigurer())
        {
        }

        public SoilEngine(EventBroker broker, ContainerConfigurer configurer)
        {
            var config = ConfigurationManager.GetSection("SoilConfig") as SoilConfig;
            InitializeContainer(configurer, broker, config);
        }

        #endregion

        #region Utilities

        private void RunStartupTasks()
        {
            var typeFinder = _containerManager.Resolve<ITypeFinder>();
            var startUpTaskTypes = typeFinder.FindClassesOfType<IStartupTask>();
            var startUpTasks = new List<IStartupTask>();
            foreach (var startUpTaskType in startUpTaskTypes)
                startUpTasks.Add((IStartupTask)Activator.CreateInstance(startUpTaskType));
            startUpTasks = startUpTasks.AsQueryable().OrderBy(st => st.Order).ToList();
            foreach (var startUpTask in startUpTasks)
                startUpTask.Execute();
        }

        private void InitializeContainer(ContainerConfigurer configurer, EventBroker broker, SoilConfig config)
        {
            var builder = new ContainerBuilder();
            _containerManager = new ContainerManager(builder.Build());
            configurer.Configure(this, _containerManager, broker, config);
        }

        #endregion

        #region Methods

        public void Initialize(SoilConfig config)
        {
            bool databaseInstalled = true;// DataSettingsHelper.DatabaseIsInstalled();
            if (databaseInstalled)
            {
                RunStartupTasks();
            }
        }

        public T Resolve<T>() where T : class
        {
            return ContainerManager.Resolve<T>();
        }

        public object Resolve(Type type)
        {
            return ContainerManager.Resolve(type);
        }

        public Array ResolveAll(Type serviceType)
        {
            throw new NotImplementedException();
        }

        public T[] ResolveAll<T>()
        {
            return ContainerManager.ResolveAll<T>();
        }

        #endregion

        #region Properties

        public IContainer Container
        {
            get { return _containerManager.Container; }
        }

        public ContainerManager ContainerManager
        {
            get { return _containerManager; }
        }

        #endregion
    }
}
